package com.study.util;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PinyinUtils {
    /**
     * @param chinese 汉字串
     * @return java.lang.String 中文拼音首字母
     * @Title: 获取中文串拼音首字母，英文字符不变
     * @methodName: getFirstSpell
     * @Description:
     * @author: FLY
     * @date: 2017-11-13 17:13
     */
    public static String getFirstSpell(String chinese) {


        StringBuffer pybf = new StringBuffer();
        char[] arr = chinese.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > 128) {
                try {
                    String[] temp = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat);
                    if (temp != null) {
                        pybf.append(temp[0].charAt(0));
                    }
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            } else {
                pybf.append(arr[i]);
            }
        }
        return pybf.toString().replaceAll("\\W", "").trim().toLowerCase();
    }

    /**
     * @param chinese 中文字符串
     * @return java.lang.String  中文拼音
     * @Title: 获取中文串拼音，英文字符不变
     * @methodName: getFullSpell
     * @Description:
     * @author: FLY
     * @date: 2017-11-13 17:15
     */
    public static String getFullSpell(String chinese) {

        StringBuffer pybf = new StringBuffer();
        char[] arr = chinese.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > 128) {
                try {
                    String[] temp = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat);
                    if (temp != null) {
                        pybf.append(temp[0]);
                    }
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            } else {
                pybf.append(arr[i]);
            }
        }
        return pybf.toString().toLowerCase();
    }

    public static void main(String[] args) {
        String string = "4 谷维素片.jpg";
        System.out.println(getFullSpell(string));

        System.out.println(getFirstSpell(parse(string)));
        // nh
        System.out.println(getFullSpell(parse(string)));
        //nihao
        System.out.println(getFullSpell(parse("4 谷维素片.jpg")));


    }

    private static String parse(String string) {
        StringBuilder stringBuilder = new StringBuilder();
        String regEx = "[`~!@#$%^&*()+=|{}:;\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]";
        Pattern c = Pattern.compile(regEx);
        Matcher mc = c.matcher(string);
        return mc.replaceAll("").trim();
    }

}